package com.jason.www.commons.entity;

import org.apache.commons.lang.StringUtils;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class GiftCardUpLoadExcelBean {
    public static final String ID="id";
    public static final String CARDNO="cardNo";
    public static final String PRICE="price";
    public static final String LICENSE="license";
    public static final String ACTIVE="active";
    public static final String MOBILE="mobile";
    public static final String DITCH="ditch";
    public static final String EXPIRE_TIME="expireTime";
    public static final String CREATE_TIME="createTime";
    public static final String LICENSE_TIME="licenseTime";

    private static final DateTimeFormatter DATE_TIME_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public static String[] getUploadExcelFieldsHeaders() {
        return new String[] {
                ID,CARDNO,PRICE,LICENSE,ACTIVE,MOBILE,DITCH,EXPIRE_TIME,CREATE_TIME,LICENSE_TIME
        };
    }

    private Long id;
    private String cardNo;
    private Float price;
    private String license;
    private Boolean active;
    private String mobile;
    private String ditch;
    private Date expireTime;
    private Date createTime;
    private Date licenseTime;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setId(String id) {
        this.id = parseNumber(id,Long.class,null);
    }

    public String getCardNo() {
        return cardNo;
    }

    public void setCardNo(String cardNo) {
        this.cardNo = cardNo;
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }

    public void setPrice(String price) {
        this.price = parseNumber(price ,Float.class,null);
    }

    public String getLicense() {
        return license;
    }

    public void setLicense(String license) {
        this.license = license;
    }

    public Boolean getActive() {
        return active;
    }

    public void setActive(Boolean active) {
        this.active = active;
    }

    public void setActive(String activeStr) {
        if(StringUtils.isNotEmpty(activeStr)){
            this.active =  Boolean.getBoolean(activeStr);
        }
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getDitch() {
        return ditch;
    }

    public void setDitch(String ditch) {
        this.ditch = ditch;
    }

    public Date getExpireTime() {
        return expireTime;
    }

    public void setExpireTime(Date expireTime) {
        this.expireTime = expireTime;
    }

    public void setExpireTime(String expireTimeStr) {
        this.expireTime = fillDateTime(expireTimeStr,expireTime);
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public void setCreateTime(String createTimeStr) {
        this.createTime = fillDateTime(createTimeStr,createTime);
    }

    public Date getLicenseTime() {
        return licenseTime;
    }

    public void setLicenseTime(Date licenseTime) {
        this.licenseTime = licenseTime;
    }

    public void setLicenseTime(String licenseTimeStr) {
        this.licenseTime = fillDateTime(licenseTimeStr,licenseTime);
    }

    private Date fillDateTime(String timeStr, Date time) {
        if(StringUtils.isNotEmpty(timeStr)){
            LocalDateTime localDateTime = LocalDateTime.parse(timeStr,DATE_TIME_FORMAT);
            return Timestamp.valueOf(localDateTime);
        }
        return time;
    }


    /**
     * parseNumber: 将文字转成Number， 不抛出错误 <br/>
     * 此方法和apache-commons里面的NumberUtils的toXX方法类似
     * @param numberString  字符串
     * @param numberClass   支持Integer.class Long.class Short.class
     * @param defaultValue  如果转换失败, 返回的默认值
     * @return
     */
    @SuppressWarnings("unchecked")
    private static <T extends Number> T parseNumber(String numberString, Class<T> numberClass, T defaultValue) {
        try {
            if(numberClass.equals(Integer.class)) {
                return (T) Integer.valueOf(numberString);
            }
            else if(numberClass.equals(Long.class)) {
                return (T) Long.valueOf(numberString);
            }
            else if(numberClass.equals(Short.class)) {
                return (T) Short.valueOf(numberString);
            }
            else if(numberClass.equals(Float.class)) {
                return (T) Float.valueOf(numberString);
            }
        } catch (NumberFormatException ex) {
            return defaultValue;
        } catch (NullPointerException ex ) {
            return defaultValue;
        } catch (Exception ex) {
            return defaultValue;
        }
        return null;
    }
}
